package com.miet.mp45.filippov.cryptoapplet.actionpanels;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingFinishedEvent;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingFinishedEventListener;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingStartedEvent;
import com.miet.mp45.filippov.cryptoapplet.actionpanels.events.ProcessingStartedEventListener;


public abstract class ActionPanel extends JPanel
{
    
    private static final long serialVersionUID = -4310478070333672037L;
    
    protected static final JFileChooser FILE_CHOOSER = new JFileChooser();
    protected static final FileNameExtensionFilter PRIVATE_KEY_FILE_FILTER = 
        new FileNameExtensionFilter("Private Key (*.pk)", "pk");
    protected static final FileNameExtensionFilter PUBLIC_KEY_FILE_FILTER = 
        new FileNameExtensionFilter("Public Key (*.pub)", "pub");
    protected static final FileNameExtensionFilter SIGNED_FILE_FILTER = 
        new FileNameExtensionFilter("Public Key (*.sig)", "sig");
    protected static final FileNameExtensionFilter CSR_FILE_FILTER = 
        new FileNameExtensionFilter("CSR (*.csr)", "csr");
    protected static final FileNameExtensionFilter CERTIFICATE_FILE_FILTER = 
        new FileNameExtensionFilter("X.509 Certificate (*.cer)", "cer");
//    protected CryptoHelper cryptoHelper = new CryptoHelper();
//    private List<ProcessingStartedEventListener> pselList = new ArrayList<ProcessingStartedEventListener>();
    private ProcessingStartedEventListener pseListener;
//    private List<ProcessingFinishedEventListener> pfelList = new ArrayList<ProcessingFinishedEventListener>();
    private ProcessingFinishedEventListener pfeListener;
    
    
    protected void addPSEListener(ProcessingStartedEventListener pseListener)
    {
        this.pseListener = pseListener;
    }
    
    protected void addPFEListener(ProcessingFinishedEventListener pfeListener)
    {
        this.pfeListener = pfeListener;
    }
    
    protected void generatePSE(final ProcessingStartedEvent event)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                pseListener.processingStarted(event);
            }
        });
    }
    
    protected void generatePFE(final ProcessingFinishedEvent event)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                pfeListener.processingFinished(event);
            }
        });
    }
    
    
    protected abstract class CryptoTask extends Thread
    {
        public abstract void doRun();

        @Override
        public final void run()
        {
            generatePSE(null);
            
            doRun();
            
            generatePFE(null);
        }
    }
    
}
